Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bible books json file that enumerates the books available in a translation #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Lucasharskamp
Copy link

@Lucasharskamp Lucasharskamp commented Nov 21, 2024

Using a C# console app I wrote just for this, I was able to create simple JSON files for each translation; a list of books, each of which is a combination of the name (string) and amount of chapters (integer). These books are listed in no particular order, or way of knowing which bible book it is, as the bible books in the JSON files don't have indexes to compare between translations. So for example, I can set Genesis as "1" since most translations don't have Genesis to begin with and there is no way of knowing which Bible book is which between translations.

The app works as follows: just start it up with the first parameter being the folder where the bibles are stored, so for example folder "C:/{path_to_git_repo}/bible-api/bibles". It will make JSON files for all Bibles, completes within 25 seconds. In the event a bible translation doesn't contain any books (which happens for some reason) an empty array is the output in the JSON file.

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace BookJsonFilesGenerator
{
    internal class Program
    {
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Arg 1 needed: directory to bible files.");
                return 1;
            }

            if (!Directory.Exists(args[0]))
            {
                Console.WriteLine("Directory in parameter does not exist.");
            }

            var jsonSettings = new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            var directory = Directory.GetDirectories(args[0]);
            foreach(var bibleTranslation in directory)
            {
                Console.WriteLine("Handling: " + bibleTranslation);
                var booksDirectory = Path.Combine(bibleTranslation, "books");
                string output;
                if (!Directory.Exists(booksDirectory))
                {
                    output = "[]";
                }
                else
                {
                    var paths = Directory.GetDirectories(booksDirectory);
                    var books = new List<Book>();
                    foreach (var path in paths)
                    {
                        var book = Path.GetFileName(path);
                        var chapterCount = Directory.GetDirectories(Path.Combine(path, "chapters")).Length;
                        books.Add(new Book()
                        {
                            Name = book,
                            Chapters = chapterCount,
                        });
                    }
                    var result = new BibleVersionBooks()
                    {
                        Books = books,
                    };
                    output = JsonSerializer.Serialize(result, jsonSettings);
                }
                var outputPath = Path.Combine(bibleTranslation, "books.json");
                File.WriteAllText(outputPath, output);

                Console.WriteLine("Output written to: " + outputPath);
            }
            
            return 0;
        }
    }

    public class BibleVersionBooks
    {
        [JsonPropertyName("books")]
        public List<Book> Books { get; set; } = default!;
    }

    public class Book
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = default!;

        [JsonPropertyName("chapters")]
        public int Chapters { get; set; }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant